<!-- 职级编辑弹窗 -->
<template>
    <el-dialog
      :title="isUpdate?'NC63药品列表':'NC63药品列表'"
      :visible="visible"
      width="70%"
      :destroy-on-close="true"
      :lock-scroll="false"
      @open="loaddata"
      @update:visible="updateVisible">
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search">
        <el-row :gutter="15" style="padding-bottom: 15px;">
          <el-col :lg="6" :md="6">
              <el-form-item label="药品名称:">
                <el-input
                  clearable
                  v-model="where.invname"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="6">
              <el-form-item label="药品编码:">
                <el-input
                  clearable
                  v-model="where.invcode"
                  placeholder=""/>
              </el-form-item>
            </el-col>
            <el-col :lg="6" :md="6">
                <div class="ele-form-actions">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="small"
                @click="querydata">查询
              </el-button>
              <el-button
                type="primary"
                icon="el-icon-check"
                class="ele-btn-icon"
                size="small"
                @click="select">选择
              </el-button>
            </div>
            </el-col>
        </el-row>
      </el-form>
      <ele-pro-table
        ref="table"
        :where="where"
        :needPage="false"
        :datasource="dataList"
        :columns="columns"
        :showTool="false"
        :selection.sync="selection"
        @row-click="handleRowClick"
        height="calc(100vh - 415px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar" v-if="false">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            >NC回写
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="false"
            >删除
          </el-button>
        </template>
      </ele-pro-table>

      <div slot="footer">
        <el-button @click="updateVisible(false)">关闭</el-button>
        <el-button v-if="false"
          type="primary"
          @click="save"
          >保存
        </el-button>
      </div>
    </el-dialog>
  </template>

  <script>
  export default {
    name: 'frmInvDocList',
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      dataItem: Object
    },
    data() {
      return {
        // 表单数据
        //form: Object.assign({status: 1}, this.dataItem),
        url: '/ygorder/ncinvlist',
        dataList:[],
        selection:[],
      // 表格列配置
      columns: [
        {
          columnKey: 'selection',
          type: 'selection',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
            prop: 'invcode',
            label: '药品编码',
            showOverflowTooltip: true,
            minWidth: 150,
            align: 'center'
          },
          {
            prop: 'invname',
            label: '药品名称',
            showOverflowTooltip: true,
            minWidth: 140,
            align: 'center'
          },
          {
            prop: 'measname',
            label: '单位',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'invspec',
            label: '规格',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'hsl',
            label: '箱规',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'sccj',
            label: '生产厂家',
            showOverflowTooltip: true,
            minWidth: 220,
            align: 'center'
          },
      ],
      // 表格搜索条件
      where: {invname:this.dataItem?this.dataItem.cpm:''},

        // 表单验证规则
        rules: {
          name: [
            {required: true, message: '请输入职级名称', trigger: 'blur'}
          ],
          status: [
            {required: true, message: '请选择职级状态', trigger: 'blur'}
          ],
          sort: [
            {required: true, message: '请输入排序号', trigger: 'blur'}
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false
      };
    },
    watch: {
      data() {
        if (this.dataItem) {
          this.where = Object.assign({}, this.dataItem);
          this.isUpdate = true;
        } else {
          this.where = {};
          this.isUpdate = false;
        }
      }
    },
    methods: {
      querydata()
      {
        if(!this.where.invcode && !this.where.invname)
        {
            this.$message.info("请药品编码或药品名称后查询。");
            return;
        }
        var url = '/ygorder/ncinvlist?ts=1';
          if(this.where.invname)
          {
            url+="&invname="+this.where.invname;
          }
          if(this.where.invcode)
          {
            url+="&invcode="+this.where.invcode;
          }
          this.$http.get(url).then(res=>{
            if (res.data.code === 0) {
                this.dataList = res.data.data;
            }
            else
            {
                this.$message.error(res.data.msg);
            }

          });

      },
      loaddata(){
        console.log(this.dataItem);
        if (this.dataItem) {
          this.where.invname = this.dataItem.cpm;
          var url = '/ygorder/ncinvlist?ts=1';
          if(this.where.invname)
          {
            url+="&invname="+this.where.invname;
          }
          if(this.where.invcode)
          {
            url+="&invcode="+this.where.invcode;
          }
          this.$http.get(url).then(res=>{
            if (res.data.code === 0) {
                this.dataList = res.data.data;
            }
            else
            {
                this.$message.error(res.data.msg);
            }

          });
        }
      },
      handleRowClick(row)
    {
      if(this.selection.length > 0){
        let del_row = this.selection.shift()
        this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
      }
      this.selection.push(row);
      this.$refs.table.toggleRowSelection(row,true)
    },
      select()
      {
        if(this.selection.length==0)
        {
            this.$message.info("请选择对应规格的药品编码。");
            return;
        }
        this.$emit('update:visible', false);
        this.$emit('done',this.selection[0].invcode,this.selection[0].invname);

      },
      /* 保存编辑 */
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.loading = true;
            this.$http[this.isUpdate ? 'put' : 'post'](this.isUpdate ? '/level/edit' : '/level/add', this.form).then(res => {
              this.loading = false;
              if (res.data.code === 0) {
                this.$message.success(res.data.msg);
                if (!this.isUpdate) {
                  this.form = {};
                }
                this.updateVisible(false);
                this.$emit('done');
              } else {
                this.$message.error(res.data.msg);
              }
            }).catch(e => {
              this.loading = false;
              this.$message.error(e.message);
            });
          } else {
            return false;
          }
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    }
  }
  </script>

  <style scoped>
  </style>
